﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public class Role
    {
        private static string CacheName = "AllRole";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Roles.ToList(), 0, 60);
            }
        }

        internal static RoleInfo TransRole2RoleInfo(Models.Role role)
        {
            RoleInfo roleInfo = new RoleInfo();

            Utility.SetObjectValueWithTheSamePropertyName(roleInfo, role);

            return roleInfo;
        }

        public static RoleInfo GetRoleByUserId(UserInfo userInfo)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            return TransRole2RoleInfo(Role_Info.Single(w => w.Id == userInfo.RoleId));
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.Role role = db.Roles.Single(w => w.Id == userInfo.RoleId);
            //    return TransRole2RoleInfo(role);
            //}
        }
        //通过用户useid 查询角色
       /* public static RoleInfo GetRoleByUserId(int userid) 
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            return TransRole2RoleInfo(Role_Info.Single(w => w.Id == userid));
        }*/
        public static string GetRoleById(int ID)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            return Role_Info.Single(w => w.Id == ID).Name;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Roles.Single(w => w.Id == ID).Name;
            //}
        }

        public static IList<RoleInfo> GetRoleInfos()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            return Role_Info.Select(c => TransRole2RoleInfo(c)).ToList();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Role>().Select(c => TransRole2RoleInfo(c)).ToList();
            //}
        }

        public static RoleInfo GetRoleInfo(int ID)
        {
            //if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            //IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            //return TransRole2RoleInfo(Role_Info.Single(t => t.Id == ID));
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransRole2RoleInfo(db.Roles.Single(t => t.Id == ID));
            }
        }

        public static RoleInfo AddRole(RoleInfo roleInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Role role = new Models.Role();
                Utility.SetObjectValueWithTheSamePropertyName(role, roleInfo);
                DatabaseHelper.Insert<Models.Role>(role);
                ToCache();
                return TransRole2RoleInfo(role);
            }
        }
        /// <summary>
        /// 通过权限名称获取对象
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public static RoleInfo GetRoleInfoByRoleName(string RoleName) 
        {
            IList<Models.Role> Role_Info = (IList<Models.Role>)CacheHelper.GetCache(CacheName);
            return TransRole2RoleInfo(Role_Info.Single(t => t.Name == RoleName));
        }
        public static void EditRole(RoleInfo roleInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Role role = db.Roles.Single(b => b.Id == roleInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(role, roleInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
    }
}
